package com.zhiche.lisa.bms.scheduled;

import com.zhiche.lisa.bms.dao.model.fee.FeePayableTemp;
import com.zhiche.lisa.bms.pojo.dto.fee.ShipParamDTO;
import com.zhiche.lisa.bms.service.capital.CapitalArrDtlService;
import com.zhiche.lisa.bms.service.charge.ChargeOffRuleService;
import com.zhiche.lisa.bms.service.fee.FeePayableService;
import com.zhiche.lisa.bms.service.fee.FeePayableTempService;
import com.zhiche.lisa.bms.service.pay.PayApplyService;
import com.zhiche.lisa.bms.service.prepay.PrepayBillService;
import com.zhiche.lisa.bms.service.price.PriceIncomingService;
import com.zhiche.lisa.bms.service.price.PricePurchaseDtlService;
import com.zhiche.lisa.bms.service.price.PricePurchaseService;
import com.zhiche.lisa.bms.service.psup.PsupBaseFleetService;
import com.zhiche.lisa.bms.service.psup.SdBaseFeePaybleFleet;
import com.zhiche.lisa.core.supports.BaseException;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class GetPriceScheduled {

    private static final Logger LOGGER = LoggerFactory.getLogger(GetPriceScheduled.class);

    @Autowired
    PriceIncomingService priceIncomingService;

    @Autowired
    PricePurchaseService pricePurchaseService;

    @Autowired
    FeePayableService feePayableService;

    @Autowired
    ChargeOffRuleService chargeOffRuleService;

    @Autowired
    PrepayBillService prepayBillService;

    @Autowired
    CapitalArrDtlService capitalArrDtlService;

    @Autowired
    private FeePayableTempService feePayableTempService;

    @Autowired
    private PayApplyService payApplyService;

    @Autowired
    private PricePurchaseDtlService pricePurchaseDtlService;

    @Autowired
    private SdBaseFeePaybleFleet sdBaseFeePaybleFleet;

    @Autowired
    private PsupBaseFleetService psupBaseFleetService;

//    /**
//     * 根据支付价格标准价为空的数据重新获取价格 定时任务
//     */
//    @Scheduled(cron = "0 0 0/1 * * ? ")
//    public void getPriceIncoming() {
//        LOGGER.info("BMS -->getPriceIncoming  start");
//        priceIncomingService.getPriceIncoming();
//    }
//
//    /**
//     * 根据收入价格标准价为空的数据重新获取价格 定时任务
//     */
//    @Scheduled(cron = "0 0 0/1 * * ?")
//    public void getPricePurchase() {
//        LOGGER.info("BMS -->getPricePurchase  start");
//        pricePurchaseService.getPricePurchase();
//    }

    /**
     * 定时更新应付账单
     * 每天的 00:30 执行
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void updateFeePayable() {
        LOGGER.info("BMS -->updateFeePayable  start");
        try {
            feePayableService.scheduledUpdateFeePayablePrice();
        } catch (Exception e) {
            LOGGER.error("BMS -->updateFeePayable  ERROR:{}", e.getMessage());
        }
        LOGGER.info("BMS -->updateFeePayable  end");
    }

    /**
     * 出账规则定时任务
     */
    //@Scheduled(cron = "0 30 3 * * ?")
    //public void chargeOffRulePayable() {
    //    LOGGER.info("BMS -->chargeOffRulePayable  start");
    //    List<ChargeOffRule> arePayableChargeOffRules = chargeOffRuleService.getArePayableChargeOffRules();
    //    if (!CollectionUtils.isEmpty(arePayableChargeOffRules)) {
    //        LOGGER.info("BMS -->chargeOffRulePayable  Size: {}", arePayableChargeOffRules.size());
    //        for (ChargeOffRule chargeOffRule : arePayableChargeOffRules) {
    //            try {
    //                chargeOffRuleService.payable(chargeOffRule.getId(), chargeOffRule, null);
    //            } catch (Exception e) {
    //                LOGGER.error("BMS -->chargeOffRulePayable  Error: {}", e.getMessage());
    //            }
    //        }
    //    }
    //    LOGGER.info("BMS -->chargeOffRulePayable  end");
    //}

    /**
     * 根据路由模式更新应付账单发运状态
     * 每两个小时执行一次
     */
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void updateFeePayableByRouteMode() {
        LOGGER.info("BMS -->updateFeePayableByRouteMode  start");
        try {
            feePayableService.updateFeePayableByRouteMode(null);
        } catch (Exception e) {
            LOGGER.error("BMS -->updateFeePayableByRouteMode  Error: {}", e.getMessage());
        }
        LOGGER.info("BMS -->updateFeePayableByRouteMode  end");
    }

    /**
     * 定时同步万金油支付申请状态
     * 每两个小时执行一次
     */
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void updateStateByFcPay() {
        LOGGER.info("BMS -->updateStateByFcPay  start");
        try {
            prepayBillService.updateStateByFcPay(null);
            capitalArrDtlService.updateStateByFcPay(null);
            payApplyService.updateOidSyncStatus();
        } catch (Exception e) {
            LOGGER.error("BMS -->updateStateByFcPay  Error: {}", e.getMessage());
        }
        LOGGER.info("BMS -->updateStateByFcPay  end");
    }

    /**
     * 定时任务转换应付账款
     * 每三分钟处理一次
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void feeTmepToPayableCommon() {
        LOGGER.info("BMS -->feeTmepToPayableCommon  start");
        try {
            List<FeePayableTemp> feeTempList = feePayableTempService.getFeeTempList();
            if (!CollectionUtils.isEmpty(feeTempList)) {
                for (FeePayableTemp record : feeTempList) {
                    if (Objects.isNull(record.getTransTypeId()) || record.getTransTypeId().equals(Long.valueOf(0))) {
                        // 没有运输方式默认为人送
                        record.setTransTypeId(6L);
                    }
                    try {
                        ShipParamDTO shipParamDTO = new ShipParamDTO();
                        BeanUtils.copyProperties(record, shipParamDTO);
                        if (record.getFleetId() != null) {
                            shipParamDTO.setFleetId(String.valueOf(record.getFleetId()));
                        }
                        boolean isSdFleetExist = psupBaseFleetService.verifyFleetIsExist(Long.valueOf(shipParamDTO.getFleetId()));
                        if (isSdFleetExist) {
                            // 处理人送数据
                            sdBaseFeePaybleFleet.dealSdFleetFeePayble(shipParamDTO);
                        } else {

                            // 判断是否慧运车后又走正常发运
                            int huiyuncheCount = pricePurchaseDtlService.getCountByOrderCode(shipParamDTO.getShipmentId());
                            if (huiyuncheCount == 0) {
                                // 非慧运车
                                feePayableService.synchroCreateFeePayable(shipParamDTO);
                            } else {
                                // 直接更新已发运 - 更新预付账单的发运状体
                                feePayableService.updateHuiyuncheLoadStatusByOrderCode(shipParamDTO.getShipmentId());
                            }
                        }
                        feePayableTempService.deleteById(record.getId());
                    } catch (BaseException e) {
                        feePayableTempService.deleteById(record.getId());
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("BMS -->feeTmepToPayableCommon  Error: {}", e.getMessage());
        }
        LOGGER.info("BMS -->feeTmepToPayableCommon  end");
    }

    /**
     * 人送更新客户运单号
     * 三分钟一次
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void updateSendDriverFeePayable() {
        LOGGER.info("BMS -->updateSendDriverFeePayable  start");
        try {
            feePayableService.updateSendDriverFeePayable();
        } catch (Exception e) {
            LOGGER.error("BMS -->updateSendDriverFeePayable  Error: {}", e.getMessage());
        }
        LOGGER.info("BMS -->updateSendDriverFeePayable  end");
    }

    /**
     * 人送生成账款
     * 三分钟一次
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void dealSendPriceToFee() {
        LOGGER.info("BMS -->dealSendPriceToFee  start");
        try {
            pricePurchaseService.dealSendPriceToFee();
        } catch (Exception e) {
            LOGGER.error("BMS -->dealSendPriceToFee  Error: {}", e.getMessage());
        }
        LOGGER.info("BMS -->dealSendPriceToFee  end");
    }
}
